var jwt = require("jsonwebtoken");

var jwtScrect = "unhejing";

var setToken = function (username, user_id) {
  return new Promise((resolve, reject) => {
    const token = jwt.sign(
      { username: username, user_id: user_id },
      jwtScrect,
      { expiresIn: "24h" }
    );
    resolve(token);
  });
};
var getToken = function (token) {
  return new Promise((resolve, reject) => {
    if (!token) {
      console.log("token is empty");
      reject({
        error: "token is empty",
      });
    } else {
      console.log("token:", token.split(" ")[1]);
      var info = jwt.verify(token.split(" ")[1], jwtScrect);
      console.log("info:", info);
      resolve(info);
    }
  });
};

module.exports = {
  setToken,
  getToken,
};
